package cn.bonoon.kernel.security;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public class AuthenticatedUserDetails {

//	private static final Logger log = LoggerFactory.getLogger(AuthenticatedUserDetails.class);

//	/**
//	 * Returns the userid of an authenticated user.
//	 * 
//	 * @return the userid if the user is logged in otherwise null
//	 */
	public Long getPrincipal() {
		return getUser().getId();
	}

	public LogonUser getUser() {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		if (null != authentication) {
			Object principal = authentication.getPrincipal();
			if (principal instanceof LogonUser) {
				return ((LogonUser) principal);
			}
		}
//		log.debug("用户没有登录或登录超时!");
		return null;
	}

	public String getPrincipalName() {
		return getUser().getDisplayName();
	}
	
	public String getUsername(){
		return getUser().getUsername();
	}
}
